﻿using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Tools
{
    /// <summary>
    /// 
    /// </summary>
    public class KeyValueData
    {
        /// <summary>
        /// 内部存储
        /// </summary>
        Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

        /// <summary>
        /// 
        /// </summary>
        char splitChar = ',', linkedChar = '=';

        /// <summary>
        /// 第一个键
        /// </summary>
        string firstKey = string.Empty;

        /// <summary>
        /// 第一个值
        /// </summary>
        string firstValue = string.Empty;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="theSplitChar"></param>
        /// <param name="theLinkedChar"></param>
        public KeyValueData(string data, char theSplitChar, char theLinkedChar)
        {
            splitChar = theSplitChar;
            linkedChar = theLinkedChar;

            if (!string.IsNullOrEmpty(data))
            {
                char c = splitChar, inner_c = linkedChar;
                string[] arr = data.Split(c);

                int index = 0;
                foreach (string s in arr)
                {
                    //s -> About=5
                    string[] inner_arr = s.Split(inner_c);
                    if (inner_arr.Length != 2)//长度判断
                        continue;
                    string _key = inner_arr[0];
                    string _value = inner_arr[1];

                    //第一个键
                    if (index == 0)
                    {
                        firstKey = _key;
                        firstValue = _value;
                    }
                    index++;

                    if (dict.ContainsKey(_key))
                        dict[_key].Add(_value);
                    else
                    {
                        List<string> _valueList = new List<string>();
                        _valueList.Add(_value);
                        dict.Add(_key, _valueList);
                    }//end if(dict.ContainsKey(_key))
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public KeyValueData(string data)
            : this(data, ',', '=')
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            if (dict.ContainsKey(key))
            {
                List<string> list = dict[key];
                if (list != null && list.Count > 0)
                    return list[0];
            }

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<string> GetValues(string key)
        {
            if (dict.ContainsKey(key))
            {
                return dict[key];
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<string> GetKeys()
        {
            Dictionary<string, List<string>>.KeyCollection keys = dict.Keys;
            List<string> list = new List<string>(keys);
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetFirstKey()
        {
            return firstKey;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, List<string>> GetDictionary()
        {
            return dict;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="forAppend"></param>
        public void Update(string key, string value, bool forAppend)
        {
            if (dict.ContainsKey(key))
            {
                if (forAppend)
                    dict[key].Insert(0, value);
                else
                {
                    List<string> l = new List<string>();
                    l.Add(value);
                    dict[key] = l;
                }
            }
            else
            {
                List<string> l = new List<string>();
                l.Add(value);
                dict.Add(key, l);
            }
        }

        /// <summary>
        /// 更新,覆盖原来的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Update(string key, string value)
        {
            Update(key, value, false);
        }

        /// <summary>
        /// 插入值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, string value)
        {
            Update(key, value, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kvp"></param>
        /// <param name="sb"></param>
        /// <returns></returns>
        void getKeyValueString(KeyValuePair<string, List<string>> kvp, StringBuilder sb)
        {
            //a=b,a=c,a=d
            foreach (string v in kvp.Value)
            {
                sb.AppendFormat("{0}{1}{2}{3}", kvp.Key, linkedChar, v, splitChar);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            dict.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair<string, List<string>> kvp in dict)
            {
                getKeyValueString(kvp, sb);
            }

            if (sb.Length > 0)
                sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }
    }
}
